/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-10-19
 * Time: 21:16
 */
//前缀和一>除自身以外数组的乘积
public class Demo {
    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] f = new int[n];
        int[] g = new int[n];

        f[0] = g[n-1] = 1;

        for(int i = 1; i <= n-1; i++)
            f[i] = f[i-1] * nums[i-1];


        for(int i = n-2; i >= 0; i--)
            g[i] = g[i+1] * nums[i+1];

        int[] answer = new int[n];

        for(int j = 0; j < n; j++)
            answer[j] = g[j] * f[j];

        return answer;
    }
}
